layui.define(["form","jquery"],function(exports){
    var form = layui.form,
               resultData=null,
               cityHtml= ' ',
               CountyHtml= ' ',
               streetHtml= ' ',
               villageHtml= ' ',
         $ = layui.jquery,
        /***
         * 三级联动
         * @param data   父id
         * @param level  级别
         * @param empty  父级重新选择子集的子集需要清空操作
         * @param resultData  结果集
         * @param areaHtml     定义插入的html参数
         */
    Address = {
        district : function (data) {

             /**只赋值一次存在缓存里**/
             if(resultData==null){
                 var url = common+"/findDicLists";
                 $.ajax({url:url, async:false,data:{district:data},success:function(result){
                         resultData = result.data.data; } });
             }



            /**
             * 市
             */
            if($("select[name='city'] option").length<1){

                for (var i = 0; i < resultData.length; i++) {

                    cityHtml += '<option value="' + resultData[i].id + '" code="' + resultData[i].districtCode + '">' + resultData[i].districtName + '</option>';

                }
                $("select[name='city']").append(cityHtml);

                form.render();
            }
            /**end**/


            /**
             * 县
             */
            if($("select[name='CountyArea'] option").length<1){

            var codeCity = $("select[name='city']").val();

            for(var z = 0; z < resultData.length; z++){
                if(resultData[z].id.toString()===codeCity){
                    for(var j=0;j<resultData[z].dicAreaList.length;j++){
                        CountyHtml += '<option value="' + resultData[z].dicAreaList[j].id + '" code="' + resultData[z].dicAreaList[j].districtCode + '">' + resultData[z].dicAreaList[j].districtName + '</option>';
                    }
                }
            }

            $("select[name='CountyArea']").append(CountyHtml);

            form.render();
            }
            /**end**/



            /**
             * 街道
             */
            if($("select[name='Rural'] option").length<1){

            var codeCounty = $("select[name='CountyArea']").val();

            for(var z = 0; z < resultData.length; z++){

                for(var j=0;j<resultData[z].dicAreaList.length;j++){
                    if(resultData[z].dicAreaList[j].id.toString()===codeCounty){
                        for(var k=0;k<resultData[z].dicAreaList[j].dicAreaList.length;k++){
                            streetHtml += '<option value="' + resultData[z].dicAreaList[j].dicAreaList[k].id + '" code="' + resultData[z].dicAreaList[j].dicAreaList[k].districtCode + '">' + resultData[z].dicAreaList[j].dicAreaList[k].districtName + '</option>';
                        }
                    }

                }
            }
            $("select[name='Rural']").append(streetHtml);

            form.render();
            }
            /**end**/



            /**
             * 居委会
             */
            if($("select[name='Neighborhood'] option").length<1){
            var codeNeighborhood = $("select[name='Rural']").val();

            for(var z = 0; z < resultData.length; z++){
                for(var j=0;j<resultData[z].dicAreaList.length;j++){
                    for(var k=0;k<resultData[z].dicAreaList[j].dicAreaList.length;k++){
                        if(resultData[z].dicAreaList[j].dicAreaList[k].id.toString()===codeNeighborhood){
                            for(var l=0;l<resultData[z].dicAreaList[j].dicAreaList[k].dicAreaList.length;l++){
                                villageHtml += '<option value="' + resultData[z].dicAreaList[j].dicAreaList[k].dicAreaList[l].id + '" code="' + resultData[z].dicAreaList[j].dicAreaList[k].dicAreaList[l].districtCode + '">' + resultData[z].dicAreaList[j].dicAreaList[k].dicAreaList[l].districtName + '</option>';
                            }
                        }
                    }
                }
            }

            $("select[name='Neighborhood']").append(villageHtml);

            form.render();
            }
            /**end**/


        },
            /***
             * 三级联动选择事件
             * @param empty  父级重新选择子集的子集需要清空操作
             */
            districtClick : function () {
                /**
                 * var parentId = data.value;//选中的值
                 * var level = elem.attributes[2].value;//获取level定义的值
                 */
                form.on('select(addressFilter)',function (data) {
                 cityHtml= ' ',CountyHtml= ' ', streetHtml= ' ', villageHtml= ' ';//清空
                 var elem = data.elem;//DOM对象
                 var empty = elem.attributes["empty"].value;//获取empty定义的值
                 var arr   = empty.split(",");
                 if(!isEmpty(arr)){//选择市清空后边的选项并重新赋值
                     $.each(arr, function(idx, obj) {
                         $("select[name="+obj+"]").html("");
                         form.render();
                     });
                 }
                     Address.district();
             })

            },
            /***
             * 三级联动输出文本框里的值
             * @param RESULT_MAP ：{xxxName:"xxx市"，xxxxCode:"153320230"}
             */
            districtCode : function () {

                var $city = $("select[name='city'] option");
                var $CountyArea = $("select[name='CountyArea'] option");
                var $Rural = $("select[name='Rural'] option");
                var $Neighborhood = $("select[name='Neighborhood'] option");
                var RESULT_MAP = {};

                /**市**/
                $.each($city,function (index,data) {
                    if(data.value==$("select[name='city']").val())
                    {
                        RESULT_MAP["cityName"]=data.innerHTML;
                        RESULT_MAP["cityCode"]=data.attributes[1].value;
                    }
                });

                /**县**/
                $.each($CountyArea,function (index,data) {
                    if(data.value==$("select[name='CountyArea']").val())
                    {
                        RESULT_MAP["countyName"]=data.innerHTML;
                        RESULT_MAP["countyCode"]=data.attributes[1].value;
                    }
                });

                /**镇**/
                $.each($Rural,function (index,data) {
                    if(data.value==$("select[name='Rural']").val())
                    {
                        RESULT_MAP["ruralName"]=data.innerHTML;
                        RESULT_MAP["ruralCode"]=data.attributes[1].value;
                    }
                });

                /**村**/
                $.each($Neighborhood,function (index,data) {
                    if(data.value==$("select[name='Neighborhood']").val())
                    {
                        RESULT_MAP["villageName"]=data.innerHTML;
                        RESULT_MAP["villageCode"]=data.attributes[1].value;
                    }
                });


                return RESULT_MAP;
            }

    };
    exports("address",Address);
})
